package com.xxx.kjds.controller;

import com.xxx.kjds.entity.Store;
import com.xxx.kjds.entity.StoreExample;
import com.xxx.kjds.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/store")
public class StoreController {
    @Autowired
    private StoreService storeService;

    /*查看网店*/
    @PostMapping(value = "/getStoreList")
    @ResponseBody
    public List<Store> GetAllStore(HttpServletRequest request) {
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        return storeService.selectByUid(uid);
    }

    /*添加店铺*/
    @PostMapping(value = "/addStore")
    @ResponseBody
    public String updateStore(HttpServletRequest request) {
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        String name = request.getParameter("name");
        String type = request.getParameter("type");

        if (name.equals("")) {
            return "false";
        }

        StoreExample storeExample = new StoreExample();
        StoreExample.Criteria criteria = storeExample.createCriteria();
        criteria.andIdIsNotNull();
        List<Store> list = storeService.selectByExample(storeExample);
        for (Store store : list) {
            if (name.equals(store.getName())) {
                return "false";
            }
        }

        Store store = new Store();
        store.setBvoId(uid);
        store.setName(name);
        store.setType(type);
        int result = storeService.insert(store);
        if (result <= 0) {
            return "false";
        }
        return "true";
    }
}
